package com.newlink.building.widget

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.text.Editable
import android.text.InputFilter
import android.text.InputFilter.LengthFilter
import android.text.TextWatcher
import android.text.method.DigitsKeyListener
import android.view.MotionEvent
import android.view.WindowManager
import android.view.inputmethod.EditorInfo
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import com.newlink.building.R

/**
 * Created by xiaodong on 2022/9/22.
 */
class Module_Phone_CustomInputDialog(context: Context) : Dialog(context, R.style.toast_setting_dialog) {

    companion object {
        const val TYPE_NAME = 0x01
        const val TYPE_ACCOUNT = 0x02
        const val TYPE_RTSP_ADDRESS = 0x03
        const val TYPE_RELAY_NAME = 0x04
        const val TYPE_RELAY_TYPE = 0x05
        const val TYPE_RELAY_HTTP_COMMAND = 0x06
        const val TYPE_RELAY_DTMF_CODE = 0x07
    }

    private lateinit var mTvTitle: TextView
    private lateinit var mBtnCanle: Button
    private lateinit var mBtnConfirm: Button
    private lateinit var mEtCenter: EditText

    private var mType = 0
    private var mTitle: String = ""
    private var mInputText: String = ""
    private var mInputHint: String = ""
    private var mInputType: Int = EditorInfo.TYPE_CLASS_TEXT
    private var mNegative: String = ""
    private var mPositive: String = ""
    private var mInputLength: Int = 0
    private var maxLength: Int = 0
    private var mDigits: String = ""
    private var negativeListener: OnClickListener? = null
    private var positiveListener: OnClickListener? = null
    private var temp: CharSequence? = null
    private var selectionStart = 0
    private var selectionEnd = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_custom_input)
        mTvTitle = findViewById(R.id.tv_title_custom_dialog)
        mTvTitle.text = mTitle

        mBtnCanle = findViewById(R.id.btn_cancel)
        mBtnCanle.text = mNegative

        mBtnConfirm = findViewById(R.id.btn_confirm)
        mBtnConfirm.text = mPositive

        mEtCenter = findViewById(R.id.et_input_center)
        mEtCenter.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
                temp = s
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                if (maxLength > 0) {
                    selectionStart = mEtCenter.selectionStart
                    selectionEnd = mEtCenter.selectionEnd

                    if (temp!!.length > maxLength) {

                        val title: String = java.lang.String.format(
                            context.resources.getString(R.string.login_max_input_text_length_warning),
                            maxLength
                        )

                        Toast.makeText(
                            context, title,
                            Toast.LENGTH_SHORT
                        ).show()
                        s!!.delete(selectionStart - 1, selectionEnd)
                    }
                }
            }
        })
        mEtCenter.setText(mInputText)
        mEtCenter.hint = mInputHint
        mEtCenter.inputType = mInputType
        if (!mDigits.isNullOrEmpty()) {
            mEtCenter.keyListener = DigitsKeyListener.getInstance(mDigits)
        }
        if (mInputLength > 0) {
            mEtCenter.filters = arrayOf<InputFilter>(LengthFilter(mInputLength))
        }

        showSoftInputFromWindow(mEtCenter)

        mBtnCanle.setOnClickListener {
            negativeListener?.onClick(mType, mEtCenter.text.toString())
        }
        mBtnConfirm.setOnClickListener {
            positiveListener?.onClick(mType, mEtCenter.text.toString())
        }
        mEtCenter.setOnTouchListener OnTouchListener@{ v, event ->
            val drawable = mEtCenter.compoundDrawables[2] ?: return@OnTouchListener false
            if (event.action != MotionEvent.ACTION_UP) return@OnTouchListener false
            if (event.x > (
                        mEtCenter.width -
                                mEtCenter.paddingRight -
                                drawable.intrinsicWidth
                        )
            ) {
                mEtCenter.setText("")
            }
            false
        }
    }

    fun setTitle(title: String): Module_Phone_CustomInputDialog {
        mTitle = title
        return this
    }

    fun setType(type: Int): Module_Phone_CustomInputDialog {
        this.mType = type
        return this
    }

    fun setMaxLength(size: Int): Module_Phone_CustomInputDialog {
        if (size <= 0) {
            return this
        }
        this.maxLength = size
        return this
    }

    fun setInputText(text: String?): Module_Phone_CustomInputDialog {
        this.mInputText = text.orEmpty()
        return this
    }

    fun setInputHint(text: String?): Module_Phone_CustomInputDialog {
        this.mInputHint = text.orEmpty()
        return this
    }

    fun setInputType(inputType: Int): Module_Phone_CustomInputDialog {
        this.mInputType = inputType
        return this
    }

    fun setInputLengh(length: Int): Module_Phone_CustomInputDialog {
        mInputLength = length
        return this
    }

    fun setDigits(digits: String): Module_Phone_CustomInputDialog {
        mDigits = digits
        return this
    }

    fun setNegativeButton(
        text: String = context.resources.getString(R.string.cancel_dialog_custom),
        listener: OnClickListener
    ): Module_Phone_CustomInputDialog {
        this.mNegative = text
        this.negativeListener = listener
        return this
    }

    fun setPositiveButton(
        text: String = context.resources.getString(R.string.save_dialog_custom),
        listener: OnClickListener
    ): Module_Phone_CustomInputDialog {
        this.mPositive = text
        this.positiveListener = listener
        return this
    }

    private fun showSoftInputFromWindow(editText: EditText) {
        window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
        editText.isFocusable = true
        editText.isFocusableInTouchMode = true
        editText.requestFocus()
    }

    interface OnClickListener {
        fun onClick(type: Int, inputText: String)
    }

    override fun dismiss() {
        super.dismiss()
    }
}
